package com.example.dao.daoImple;

import com.example.dao.CommentDao;
import com.example.entity.Comment;
import com.example.entity.CommentLike;
import com.example.entity.vo.CommentVO;
import com.example.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @Description: 评论持久层实现类
 * @Date: 2022/11/15 16:10
 * @Author: SinkDream
 */
public class CommentDaoImpl implements CommentDao {
    @Override
    public Comment getComment(Long id) {
        String sql = "select * from tb_comment where id = ?";
        return JDBCUtil.getInstance(Comment.class,sql,id);
    }

    @Override
    public List<CommentVO> findByArticleIdParentId(Long articleId, Long parentId) {
        String sql = "select c.id,c.user_id,c.article_id,c.content,c.parent_comment_id,c.likes,c.create_time,c.state,u.nick_name,u.avatar from tb_comment c left join tb_user u on c.user_id = u.id where c.article_id=? and c.parent_comment_id=? and c.state=? order by c.create_time desc";
        return JDBCUtil.getForList(CommentVO.class,sql,articleId,parentId,0);
    }

    @Override
    public int addComment(Comment comment) {
        String sql = "insert into tb_comment(user_id,article_id,content,parent_comment_id,create_time) values(?,?,?,?,?)";
        return JDBCUtil.ALLCUD(sql,comment.getUserId(),comment.getArticleId(),comment.getContent(),comment.getParentCommentId(),comment.getCreateTime());
    }

    @Override
    public int countComment(Long articleId) {
        String sql = "select count(1) from tb_comment where article_id=? and state=?";
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConn();
            psmt = conn.prepareStatement(sql);
            psmt.setLong(1,articleId);
            psmt.setInt(2,0);

            rs = psmt.executeQuery();
            rs.next();
            return rs.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs,psmt,conn);
        }
        return 0;
    }

    @Override
    public int deleteComment(Long id) {
        String sql = "update tb_comment set state=? where id=?";
        return JDBCUtil.ALLCUD(sql,1,id);
    }

    @Override
    public int updateCommentLikes(Comment comment) {
        String sql = "update tb_comment set likes=? where id=?";
        return JDBCUtil.ALLCUD(sql,comment.getLikes(),comment.getId());
    }

    @Override
    public List<Comment> listCommentByParentId(Long parentId) {
        String sql = "select * from tb_comment where parent_comment_id=?";
        return JDBCUtil.getForList(Comment.class,sql,parentId);
    }

    @Override
    public int addCommentLike(CommentLike commentLike) {
        String sql = "insert into tb_like(user_id,comment_id) values(?,?)";
        return JDBCUtil.ALLCUD(sql,commentLike.getUserId(),commentLike.getCommentId());
    }

    @Override
    public int deleteCommentLike(CommentLike commentLike) {
        String sql = "delete from tb_like where comment_id = ? and user_id = ?";
        return JDBCUtil.ALLCUD(sql,commentLike.getCommentId(),commentLike.getUserId());
    }

    @Override
    public List<CommentLike> listCommentLike(Long userId) {
        String sql = "select * from tb_like where user_id = ?";
        return JDBCUtil.getForList(CommentLike.class,sql,userId);
    }

    @Override
    public CommentLike getCommentLike(Long id) {
        String sql = "select * from tb_like where id = ?";
        return JDBCUtil.getInstance(CommentLike.class,sql,id);
    }
}
